package com.sky.task;

import com.sky.constant.MessageConstant;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
// @Component
public class OrderTask {

    // @Autowired
    private OrderMapper orderMapper;

    /**
     * 处理订单超时未支付的状态
     */
    // @Scheduled(cron = "* */5 * * * ?")
    // @Scheduled(cron = "*/5 * * * * ?")
    public void orderTimeout() {
        log.info("定时任务执行了==处理订单超时未支付的状态==");
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
        List<Orders> orders = orderMapper.getStatusAndOrderTimeByTime(Orders.PENDING_PAYMENT,time);
        if (orders != null && orders.size() > 0) {
            orders.forEach(order -> {
                order.setStatus(Orders.CANCELLED);
                order.setCancelTime(LocalDateTime.now());
                order.setCancelReason(MessageConstant.AUTO_CANCELED_DUE_TO_TIMEOUT);
                orderMapper.update(order);
            });
        }
    }

    /**
     * 处理订单派送中未完成的状态
     */
    // @Scheduled(cron = "*/5 * * * * ?")
    // @Scheduled(cron = "0 0 1 * * ?")
    public void handleIncompleteOrders() {
        log.info("定时任务执行了==处理订单派送中状态");
        LocalDateTime time = LocalDateTime.now().plusMinutes(-60);
        List<Orders> orders = orderMapper.getStatusAndOrderTimeBySendTime(Orders.DELIVERY_IN_PROGRESS,time);
        if (orders != null && orders.size() > 0) {
            orders.forEach(order -> {
                order.setStatus(Orders.COMPLETED);
                orderMapper.update(order);
            }); 
        }
        
    }


}



















